<?php 
/**
 * 邮件列表
 * @copyright   Copyright (c) 2006 - 2010 DZH 友拓软件友拓通
 * @category   	系统信息
 * @package  	Action
 * @author    	李恺弘
 * @version 	2.1,2012-07-22
 */

class EmailListPublicAction extends BasicCommonAction {
	  
	public 	$_sortBy 			= 'id';  ///默认排序字段  
	 
	///已发货列表
	public function index() {	
		$this->basicList('and is_sent=2'); 
		$this->displayIndex();
	}	
	
	///已发货列表
	public function unsentEmailList() {	 
		$this->basicList('and is_sent=1');  
		///display
		$this->displayIndex(__FUNCTION__);
	}	
	
	///已发货列表
	public function deleteEmailList() {	 
		$this->basicList('and is_sent=3');   
		///display
		$this->displayIndex(__FUNCTION__);
	}	
	
	
	public function basicList($where){
		///获取当前Action名称
	 	$name = $this->getActionName(); 
 		///获取当前模型
		$model 	= D($name);    
		///条件
		$opert	=	array(
		'field'=>'*,case email_type
     	when 1 then \'Orders\'
        when 2 then \'LoadContainer\'
        when 3 then \'Instock\'
        when 4 then \'SaleOrder\'
        when 5 then \'Delivery\'
        else 6 end as object_type',
        'where'=>_search($this->_default_where,$this->_default_post).' and to_hide=1 '.$where);
		///格式化+获取列表信息  
		$list	=	_list(&$model,&$opert);  
		$linkModel	=	D('AbsStat');  
		foreach ((array)$list as $key=>$row) { 
			$list[$key]	=	$linkModel->objectTypeCommentSubsidiary($row);   
		}
		$list	=	_formatList($list);
		///assign
		$this->assign('list',$list);
	}
	
	///更新
	public function edit() {
		///获取当前Action名称
	 	$name = $this->getActionName();
 		///获取当前模型
		$model 	= D($name);   
		///当前主键
		$pk		=	$model->getPk ();
		$id 	= 	intval($_REQUEST[$pk]);
		$info	=	$model->find($id); 
		
		///给所有权限
		if ($_SESSION[C('ADMIN_AUTH_KEY')]==false){
				$_SESSION[C('ADMIN_AUTH_KEY')]			=	true;
				$a	=	true;
		} 
		if ($id>0) {  ///发单个
			$this->sentEmailArray($info,$id,&$model); 
			///回退权限
			if ($a){
				$_SESSION[C('ADMIN_AUTH_KEY')]			=	false;
			} 
		}else{ 
			///用于群发
			$list	=	$model->where('is_sent=1')->select();  
			foreach ((array)$list as $key=>$row) {   
				$this->sentEmailArray($row,$row['id'],&$model); 
			} 
			///回退权限
			if ($a){
				$_SESSION[C('ADMIN_AUTH_KEY')]			=	false;
			} 
			echo 'sent OK';
			exit;
			$this->error(L('_ERROR_'));
		}  
	}
	
	public function sentEmailArray($info,$id,&$model){
		$modelArray	=	array( 
						1=>'Orders',
						2=>'LoadContainer',
						3=>'Instock',
						4=>'SaleOrder',
						5=>'Delivery',
		); 
		
		$flow		=	A($modelArray[$info['email_type']]); 
		$flow->autoLang($modelArray[$info['email_type']]);
		$flow->setName($modelArray[$info['email_type']]); 
		$_GET['id']	=	$info['object_id'];
		$flow->sentEmail($modelArray[$info['email_type']]);  
		$condition 	= 'id='.$id; 
		$today 	= date("Y-m-d H:m:s");                           /// 20010310 
		$data 	= array('is_sent'=>2,'send_time'=>$today);    
		$list	=	$model->where($condition)->setField($data);    
///		$model->getLastSql(); 
	}
		
	
	public function _after_edit(){
    	$this->success(L('_OPERATION_SUCCESS_')); 	
    } 
    
	 ///删除
    public function delete(){  
	    	///获取当前Action名称
		 	$name = $this->getActionName();
	 		///获取当前模型
			$model 	= D($name);   
			///当前主键
			$pk		=	$model->getPk ();
			$id 	= 	intval($_REQUEST[$pk]);
			if ($id>0) {  
				$condition 	= $pk.'='.$id; 
				$today 	= date("Y-m-d H:m:s");                           /// 20010310 
				$data 	= array('is_sent'=>3,'delete_time'=>$today);   
				$list	=	$model->where($condition)->setField($data);   
				///如果删除操作失败提示错误
				if ($list==false) {
					$this->error(L('data_right_del_error'));
				}
			}else{
				$this->error(L('_ERROR_'));
			}  
    }   
      
}